package com.yiyuan.app.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.guaike.core.mybatis.plugins.BaseDAO;
import com.yiyuan.app.entity.po.MRecordOccupyPO;
import com.yiyuan.app.mapper.MRecordOccupyExtMapper;
import com.yiyuan.app.mapper.MRecordOccupyMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
@AllArgsConstructor
public class MRecordOccupyDAO extends BaseDAO<MRecordOccupyMapper, MRecordOccupyPO> {

    private final MRecordOccupyExtMapper mRecordOccupyExtMapper;

    public MRecordOccupyPO findUserOccupyingOne(Long userId, Long roomId, String positionNo) {
        LambdaQueryWrapper<MRecordOccupyPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MRecordOccupyPO::getUserId, userId);
        queryWrapper.eq(MRecordOccupyPO::getRoomId, roomId);
        queryWrapper.eq(MRecordOccupyPO::getPositionNo, positionNo);
        queryWrapper.isNotNull(MRecordOccupyPO::getEndTime);
        queryWrapper.eq(MRecordOccupyPO::getIsSettle, Boolean.FALSE);
        queryWrapper.gt(MRecordOccupyPO::getEndTime, LocalDateTime.now());
        return getBaseMapper().selectOne(queryWrapper);
    }


    public Long checkOtherUserOccupying(Long userId, Long roomId) {
        LambdaQueryWrapper<MRecordOccupyPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(MRecordOccupyPO::getUserId, userId);
        queryWrapper.eq(MRecordOccupyPO::getRoomId, roomId);
        queryWrapper.isNotNull(MRecordOccupyPO::getEndTime);
        queryWrapper.gt(MRecordOccupyPO::getEndTime, LocalDateTime.now());
        return getBaseMapper().selectCount(queryWrapper);
    }

    public int addPinecone(Long id, Integer pinecone) {
        return mRecordOccupyExtMapper.addPinecone(id, pinecone);
    }

    public int settle(Long id, Integer pinecone) {
        return mRecordOccupyExtMapper.settle(id, pinecone);
    }


    public List<MRecordOccupyPO> findAllRepairByRoom(Long roomId) {
        LambdaQueryWrapper<MRecordOccupyPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MRecordOccupyPO::getRoomId, roomId);
        queryWrapper.isNotNull(MRecordOccupyPO::getRepairTime);
        return getBaseMapper().selectList(queryWrapper);
    }

    public List<MRecordOccupyPO> findAllNormalNoEndByRoom(Long roomId) {
        LambdaQueryWrapper<MRecordOccupyPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MRecordOccupyPO::getRoomId, roomId);
        queryWrapper.isNull(MRecordOccupyPO::getRepairTime);
        queryWrapper.isNotNull(MRecordOccupyPO::getEndTime);
        queryWrapper.gt(MRecordOccupyPO::getEndTime, LocalDateTime.now());
        return getBaseMapper().selectList(queryWrapper);
    }

    public int updateNormal(Long id, LocalDateTime endTime) {
        LambdaUpdateWrapper<MRecordOccupyPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(MRecordOccupyPO::getEndTime, endTime);
        updateWrapper.set(MRecordOccupyPO::getRepairTime, null);
        updateWrapper.set(MRecordOccupyPO::getRemainTime, null);
        updateWrapper.eq(MRecordOccupyPO::getId, id);
        return getBaseMapper().update(new MRecordOccupyPO(), updateWrapper);
    }

    public int updateRepair(Long id, Integer remainMinute) {
        LambdaUpdateWrapper<MRecordOccupyPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(MRecordOccupyPO::getEndTime, null);
        updateWrapper.set(MRecordOccupyPO::getRepairTime, LocalDateTime.now());
        updateWrapper.set(MRecordOccupyPO::getRemainTime, remainMinute);
        updateWrapper.eq(MRecordOccupyPO::getId, id);
        return getBaseMapper().update(new MRecordOccupyPO(), updateWrapper);
    }

    public int updateSettleEnd(Long id) {
        LambdaUpdateWrapper<MRecordOccupyPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(MRecordOccupyPO::getEndTime, LocalDateTime.now());
        updateWrapper.set(MRecordOccupyPO::getIsSettle, Boolean.TRUE);
        updateWrapper.eq(MRecordOccupyPO::getId, id);
        return getBaseMapper().update(new MRecordOccupyPO(), updateWrapper);
    }

    public MRecordOccupyPO findByIdLock(Long id) {
        return mRecordOccupyExtMapper.findByIdLock(id);
    }
}
